$(function() {
	   isEnoughPay = function(){
	       //判断用户是否实名
	       var status;
	       $.TsignAjax({
               url: "./user/account!getAccountInfo2.xml",
               dataType: "json",
               async:false,
               success: function(data) {
                   status = data.status;
               }
           })
           if (status != 9 && 1 == $("#selfSign").val()) {
               showAlert("您还未进行实名认证，请先进行<a style='text-decoration:underline' href='./user/account!page?skip=identify'>实名认证</a>");
               return;
           }
		  	var payType =checkPayType();
	    	var count = payType == 0 ? parseInt($("#billCount_1").html()) : parseInt($("#billCount_2").html());
		   //if(!getRemainSignNum){
			//   $.billingLogin();
		   //}
	    //	getRemainSignNum(null,messageHanddle,count);
		   //若获取计费信息异常，也进行到签署页面
		   //
		   if(count > 0){
			   $.TsignAjax({
					   url:"./user/account!getBillingRemain.xml",
					   async:false,
					   success:function(jsonData){
						   if(!jsonData["data"]["sumInfo"]){
							   showAlert("您还未定制任何计费套餐");
							   return;
						   }
						   if(jsonData["data"]["sumInfo"]["remain"] >= count){
							   saveOrNext();
						   }else{
							   shouldRecharge(null);
						   }
					   },
					   error:function(jsonData){
						   showAlert("计费连接异常");
					   }
				   }
			   )
		   }else{
			   saveOrNext();
		   }



	    }
	 messageHanddle = function(args){
		 if(remains >= args){
			 saveOrNext();
		 } else{
			 shouldRecharge(null);

		 }

	  }
	 checkPayType = function(){
		  var payType = 0;
		 $(".signPay").each(function(index){
		    	if("false" == $(this).attr("ispay")){
		    		if(index == $(".signPay").length-1){
		    		showAlert("请选择签署付款方式");
		    		payType = -1;
		    		}
		    	}else{
		    		var one = $(this).attr("id");
		    		payType = parseInt(one.substring(one.length-1,one.length))-1;
		    		return false;
		    	}
		    });
		 return payType;
		 
	 }
	saveOrNext = function(){
//	    var fileCount = $(".fileNumber").length-1;
//
//	    if(fileCount==0){
//	        showAlert("请至少上传一个文档!");
//	        return;
//	    }
	    
//	    var sxdays = $("#sxdays").val();
//	    if(sxdays){
//	    	var numReg = /^\d{1,3}$/;
//	    	if(!numReg.test(sxdays)){
//	    		showAlert("请输入正确的有效时间");
//	    		return;
//	    	}
//	    }
	    var contactCount = $(".whiteTopBord").length;
	    if (contactCount <= 0) {
	    	showAlert("请至少填写一个联系人");
	    	return;
	    }
	    var orderType = $("#orderType").val();
	    if (orderType == 1) {
	    	if (contactCount < 2) {
	    		showAlert("按顺序签名需填写两个收件人或以上");
		    	return;
	    	}
	    }
	    var payType =checkPayType();
	   if(payType < 0){
	    	return ;
	    }
	    var emailRegExp = new RegExp("^([a-zA-Z0-9]+[_|\_|\.\-]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.\-]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$");
	    var mobileRegExp = /^1\d{10}$/;
	    var wrongNumber = 0;
	    
	    //检验联系人信息
	    for (var i = 1; i <= contactCount; i++) {
            var email = $("#email_" + i).val();
            var name = $("#name_" + i).val();
            var phone = $("#phone_" + i).val();

            if (email != "") {
                if (!emailRegExp.test(email) && !mobileRegExp.test(email)) {
                	$("#email_"+i).css("color","red");
                    wrongNumber++;
                }
            }else{
            	$("#email_"+i).css("border","1px solid red");
            	wrongNumber++;
            }
            
//            if(name==""){
//            	$("#name_"+i).css("border","1px solid red");
//                wrongNumber++;
//            }
            
//            if(phone!=""){
//				if (!mobileRegExp.test(phone)) {
//			   		$("#phone_"+i).css("border","1px solid red");
//			      	wrongNumber++;
//			   	}
//            }else{
//            	$("#phone_"+i).css("border","1px solid red");
//            	wrongNumber++;
//            }
	    }
	    
        if (wrongNumber == 0) {
//        	var emailTitle = $("#emailTitle").val();
//        	if(!emailTitle){
//        		$("#emailTitle").css("border","1px solid red");
//        		showAlert("请填写标题");
//        		return;
//        	}
	       	for (var i = 1; i <= contactCount; i++) {
	       		 var email = $("#email_" + i).val();
	             var name = $("#name_" + i).val();
	             var phone = $("#phone_" + i).val();
	             if (email == "" && name == "" && phone == "") {
	                 delRow(i);
	             }
	        }
	                
	       	contactCount = $(".whiteTopBord").length;
	        var sameCount = 0;
	        for(i=1;i<=contactCount;i++){
            	var accountUid = $("#accountUid_"+i).val();
            	var email = $("#email_"+i).val();
             	for(x=2;x<=contactCount;x++){
                	if(x!=i){
                    	var accountUidx = $("#accountUid_"+x).val();
                    	var emailx = $("#email_"+x).val();
                     	if(("" != accountUid && "" != accountUidx && accountUid == accountUidx) || email == emailx){
                       		sameCount++; 
                         	$("#email_"+i).css("color","red");
                        	$("#email_"+x).css("color","red");
                        	$("#phone_"+i).css("color","red");
                        	$("#phone_"+x).css("color","red");
                     	}
	                 }
	             }
         	}
         	if(sameCount!=0){
	            showAlert("存在相同的收件人，请修改");
	            return;
	        }
			$("#lines").val(contactCount);
			
			var orderType = $("#orderType").val();
			var signType = $("#orderType").val();
			var nextType =2;
			var sendArray = new Array();
			$(".whiteTopBord").each(function(index){
				var sendJson = {};
				page = index + 1;
				sendJson["receiver"] = $("#email_" + page).val();
				var operaType = $("#operaType_" + page).val();
				if (operaType == 2) {
					sendJson["type"] = 1;
				} else {
					nextType = 5;
					if (orderType == 1) {
						sendJson["type"] = 2;
					} else {
						sendJson["type"] = 0;
					}
				}
				sendJson["isSendMsg"] = $("#needVideo_" + page).val();
				sendArray[index] = sendJson;
			});
			
			var sends = JSON.stringify(sendArray);
			var signdocId = $("#signdocId").val();
			var postData = "signdocId=" + signdocId + "&sends=" + sends + "&edit=" + (new Date()).valueOf();
			showblockUI();
			$.ajax({
				url : "sign!addReceiver.xml",
				type : "POST",
				data : postData,
				dataType : "xml",
				timeout : 300000,
				success:function(data){
					closeAlert();
					var result = $(data).find("result >resultMsg").text();
					if (result == null || result == "") {
						showAlert("系统出错，请稍后重试");
						return;
					}
					var jsonData = JSON.parse(result);
					var errCode = jsonData["errCode"];
					if (errCode > 0) {
						var errShow = new Boolean(jsonData["errShow"]);
						if (errShow == true) {
							showAlert(jsonData["msg"]);
						} else {
							showAlert("系统出错，请稍后重试");
						}
						return;
					} else {
						if ($("#saveDraft").val() == 1) {
							showUrlMust("保存成功", "doc!page?skip=doclist-view&type=1");
							return;
						}
						var selfSign = $("#selfSign").val();
						sessionStorage.signType = '';
					    if(1 == selfSign){
							//var doc = {
							//	"docId":$("#signdocId").val(),
							//	"nextType":nextType,
							//	"payMethod":payType,
							//	"sender":"",
							//	"returnContractType":$("#signType").val(),
							//	"pageType":"owner"
							//}
							//sessionStorage.setItem($("#signdocId").val(),JSON.stringify(doc));
							//window.location.href = "sign!page?skip=ownerSign2&docId="+$("#signdocId").val();
					    	$.TsignSubmit({
					    		url:"sign!page",
					    		input:[
					    		       	{name:"skip",value:"ownerSign"},
					    		       	{name:"page",value:"owner"},
					    		       	{name:"signdocId",value:$("#signdocId").val()},
					    		       	{name:"nextType",value:nextType},
					    		       	{name:"payType",value:payType},
										{name:"signType",value:$("#signType").val()}
								]
					    	});
					    //	window.location.href = "sign!page?skip=ownerSign&page=owner&signdocId=" + $("#signdocId").val()+"&nextType="+nextType;
					    }else{
					    	send(signdocId,payType);
					    }
					}
				}
			});
			
//			if(action =="document.do?method=toSaveContract"){
//			    var selfSign = $("#selfSign").val();
//			    if(2!=selfSign){
//			    	if(1==selfSign){
//			    		showAlertSelfSign("您已勾选了“需要签名”，是否现在进行签署？");
//				    }else{
//				    	showAlertSelfSignNSS("是否需要自己签署？");
//				    }
//			    }else{
//			    	form2.action = action;
//		           	form2.submit();
//			    }
//		    }else{
//		    	form2.action = action;
//	           	form2.submit();
//		    }
	    } else {
        	showAlert("请检查并修改收件人列表中错误");
        }
	}
	

	
	send = function(signdocId,payType) {
		showblockUI();
		var postData = "signdocId=" + signdocId +"&payMethod="+parseInt(payType)+ "&edit=" + (new Date()).valueOf();
		$.ajax({
			url : "sign!sendEmail.xml",
			type : "POST",
			data : postData,
			dataType : "xml",
			timeout : 300000,
			success : function(data) {
				closeAlert();
				var result = $(data).find("result >resultMsg").text();
				if (result == null || result == "") {
					showAlert("系统出错，请稍后重试");
					return;
				}
				var jsonData = JSON.parse(result);
				var errCode = jsonData["errCode"];
				if (errCode > 0) {
					var errShow = new Boolean(jsonData["errShow"]);
					if (errShow == true) {
						showAlert(jsonData["msg"]);
					} else {
						showAlert("系统出错，请稍后重试");
					}
					return;
				} else {
					//发起人和接收者均无需签名，此合同已完成签署,跳转至已完成
					if($("#selfSign").val()==0 && $("#operaType_1").val()==2){
						showUrlMust("邀请签章邮件已成功发送给您的好友，请耐心等待", "doc!page?skip=doclist-view&type=2");
						return;
					}
					if($("#nextType").val()==2){
						showUrlMust("邀请签章邮件已成功发送给您的好友，请耐心等待", "doc!page?skip=doclist-view&type=2");
						return ;
					}
					showUrlMust("邀请签章邮件已成功发送给您的好友，请耐心等待", "doc!page?skip=doclist-view&type=5");
				}
			}
		});
	}
	
	showContactList = function () {
		//$("#content_contact").show();
	}
	
	addRow = function() {
		$('.select_ul').hide();
		for (var i = 0; i < 100; i++) {
			var page = i + 1;
			if($("#address_" + page).length > 0){
				continue;
			}
																								//title='拖动以更改顺序'draggable='true' ondragstart='dragContact(event," + page + ")'
			$("#addResseeList").append("<tr class='whiteTopBord' id='address_" + page + "'>" 
			+ "<td title='拖动以更改顺序' draggable='true' ondragstart='dragContact(event, " + page + ")' height='39' align='center'><img src='./images_2.0/fasong1_53.png' /></td>" 
			+ "<td align='center'><input id='order_" + page + "' name='order_" + page + "' type='hidden' value='" + page + "'><span class='numberList'>" + page + "</span></td>" 
			+ "<td>" 
			+ "<input type='text' id='email_" + page + "' name='email_" + page + "' class='email' value='' onblur='getReceiveInfoById(" + page + ",this.value)'/><input type='hidden' id='accountUid_" + page + "' name='accountUid_" + page + "'/>" 
			+ "<div style='position: absolute;'><div style='position: absolute; left: 238px; top: -26px;'>" 
			+ "<a href='javascript:showContactList(" + page + ");' style='display:none'>" 
			+ "<img src='./images_2.0/fasong_37.png' /></a>" 
			+ "</div></div>" 
			+ "</td>" 
			+ "<td><input id='name_" + page + "' name='name_" + page + "' type='text' class='name' maxlength='10'  value='' readonly='readonly' disabled='disabled'/></td>" 
			+ "<td><div name='video' id='need_video_div_" + page + "' class='ml5 video_no' onclick='needOrNotVideo(" + page + ");'>" 
			+ "<input class='video_input' type='hidden' id='needVideo_" + page + "' name='needVideo_" + page + "' value='0' />" 
			+ "</div></td>" 
			+ "<td><input id='phone_" + page + "' name='phone_" + page + "' type='text'  class='name' value=''  maxlength='11' onkeyup='value=value.replace(/[^\d]/g,'')' onafterpaste='this.value=this.value.replace(/\d/g,'');' readonly='readonly' disabled='disabled'/></td>" 
			+ "<td>" 
			+ "<input class='operaType' type='hidden' name='operaType_1' id='operaType_" + page + "' value='1' />" 
			+ "<div class='pr'>" 
			+ "<div class='select_div' id='select_div_" + page + "' onclick='showSelect(" + page + ");'>" 
			+ "<img src='./images_2.0/fasong1_41.png' class='mr2' />需要签署" 
			+ "</div>" 
			+ "<ul class='select_ul pa1' style='display: none;' id='ul_select_" + page + "'>" 
			+ "<li onclick='selectOpera(" + page + ",1);'><img src='./images_2.0/fasong1_41.png' class='mr2' />需要签署</li>" 
			+ "<li onclick='selectOpera(" + page + ",2);'><img src='./images_2.0/fasong1_44.png' class='mr2'>接受副本</li>" 
			+ "</ul>" 
			+ "</div>" 
			+ "</td>" 
			+ "<td><span id='userStatus_" + page + "'></span></td>" 
			+ "<td><a href='javascript:delRow(" + page + ")'><div class='del_img'></div></a></td>" 
			+ "</tr>");
			break;
		}
		 $("#billCount_1").html(calculateSignCount());
	}
	
	delRow = function(page) {
		
		if ($(".whiteTopBord").length == 1) {
			showAlert("请至少填写一个联系人");
			return;
		}
		$("#address_" + page).remove();
		$(".whiteTopBord").each(function(index){
			i = index + 1;
			if($("#address_" + i).length > 0){
				
			}else{
				page = i + 1;
				var email = $("#email_" + page).val();
				var accountUid = $("#accountUid_" + page).val();
				var name = $("#name_" + page).val();
				var phone = $("#phone_" + page).val();
				var needVideo = $("#needVideo_" + page).val();
				var operaType = $("#operaType_" + page).val();
				var userStatus = $("#userStatus_" + page).html();
				if (needVideo == 1) {
					needVideo = 0;
				} else {
					needVideo = 1;
				}
				$("#address_" + page).remove();
				$("#addResseeList").append("<tr class='whiteTopBord' id='address_" + i + "'><td title='拖动以更改顺序' draggable='true' ondragstart='dragContact(event, " + page + ")' height='39' align='center'><img src='./images_2.0/fasong1_53.png' /></td><td align='center'><input id='order_" + i + "' name='order_" + i + "' type='hidden' value='" + i + "'><span class='numberList'>" + i + "</span></td><td><input type='text' id='email_" + i + "' name='email_" + i + "' class='email' value='"+ email +"' onblur='getReceiveInfoById(" + i + ",this.value)'/><input type='hidden' id='accountUid_" + page + "' name='accountUid_" + page + "' value='" + accountUid + "'/><div style='position: absolute;'><div style='position: absolute; left: 238px; top: -26px;'><a href='javascript:showContactList(" + i + ");' style='display:none'><img src='./images_2.0/fasong_37.png' /></a></div></div></td><td><input id='name_" + i + "' name='name_" + i + "' type='text' class='name' maxlength='10'  value='"+ name +"' readonly='readonly' disabled='disabled'/></td><td><div name='video' id='need_video_div_" + i + "' class='ml5 video_no' onclick='needOrNotVideo(" + i + ");'><input class='video_input' type='hidden' id='needVideo_" + i + "' name='needVideo_" + i + "' value='"+ needVideo +"' /></div></td><td><input id='phone_" + i + "' name='phone_" + i + "' type='text'  class='name' value='"+ phone +"'  maxlength='11' onkeyup='value=value.replace(/[^\d]/g,'')' onafterpaste='this.value=this.value.replace(/\d/g,'');' readonly='readonly' disabled='disabled'/></td><td><input class='operaType' type='hidden' name='operaType_1' id='operaType_" + i + "' value='"+ operaType +"' /><div class='pr'><div class='select_div' id='select_div_" + i + "' onclick='showSelect(" + i + ");'><img src='./images_2.0/fasong1_41.png' class='mr2' />需要签署</div><ul class='select_ul pa1' style='display: none;' id='ul_select_" + i + "'><li onclick='selectOpera(" + i + ",1);'><img src='./images_2.0/fasong1_41.png' class='mr2' />需要签署</li><li onclick='selectOpera(" + i + ",2);'><img src='./images_2.0/fasong1_44.png' class='mr2'>接受副本</li></ul></div></td><td><span id='userStatus_" + i + "'>"+ userStatus +"</span></td><td><a href='javascript:delRow(" + i + ")'><div class='del_img'></div></a></td></tr>");
				needOrNotVideo(i);
				selectOpera(i, operaType);
			}
		});
		$("#billCount_1").html(calculateSignCount());
	}
	
	needOrNotVideo = function(index){
		var needVideo = $("#needVideo_"+index).val();
		if(0==needVideo){
			$("#need_video_div_"+index).addClass("video_yes").removeClass("video_no");
			$("#needVideo_"+index).val(1);
		}else if(2==needVideo){
			$("#need_video_div_"+index).addClass("video_yes").removeClass("video_no");
			$("#need_DoubleVideo_div_"+index).addClass("doublevideo_no").removeClass("doublevideo_yes");
			$("#needVideo_"+index).val(1);
			$("#doubleVideo_ckeck").removeAttr("checked");
		}else if(1==needVideo){
			$("#need_video_div_"+index).addClass("video_no").removeClass("video_yes");
			$("#needVideo_"+index).val(0);
		}
		
		isAllCheck("video");
	}
	
	allVideo = function(obj){
		if(obj.checked){
			$("div[name='video']").each(function(i){
				$("#need_video_div_"+(i+1)).addClass("video_yes").removeClass("video_no");
				$("#needVideo_"+(i+1)).val(1);
			});
			$("div[name='doubleVideo']").each(function(i){
				$("#need_DoubleVideo_div_"+(i+1)).addClass("doublevideo_no").removeClass("doublevideo_yes");
			});
			
			isAllCheck("doubleVideo");
	  	}else{
			$("div[name='video']").each(function(i){
				$("#need_video_div_"+(i+1)).addClass("video_no").removeClass("video_yes");
				$("#needVideo_"+(i+1)).val(0);
			});
 		}
	}
	
	isAllCheck = function(type){
		if("mobile"==type){
			 $("#mobile_ckeck").attr("checked",$("div[name='mobile']").length == $(".mobile_yes").length ? true : false);
		}else if("video"==type){
			$("#video_ckeck").attr("checked",$("div[name='video']").length == $(".video_yes").length ? true : false);
		}else if("doubleVideo"==type){
			if($("#doubleVideo_ckeck")!="undefine"){
				$("#doubleVideo_ckeck").attr("checked",$("div[name='doubleVideo']").length-1 == $(".doublevideo_yes").length ? true : false);
			}
		}
	}
	
	showSelect = function(index) {
		$('.select_div').show();
		$('.select_ul').hide();
		$("#ul_select_" + index).css("display") == "none" ? $("#ul_select_" + index).show() : $("#ul_select_" + index).hide();
		$("#select_div_" +  (index + 1)).hide();
	}
	
	selectOpera = function(divIndex, liIndex) {
		$('.select_ul').hide();
		if(1==liIndex){
			$("#select_div_" + divIndex).html("<img src=\"./images_2.0/fasong1_41.png\" class=\"mr2\"/>需要签署");
			$("#operaType_"+divIndex).val(1);
		}else{
			$("#select_div_" + divIndex).html("<img src=\"./images_2.0/fasong1_44.png\" class=\"mr2\"/>接受副本");
			$("#operaType_"+divIndex).val(2);
		}
		$("#select_div_" +  (divIndex + 1)).show();
		$("#billCount_1").html(calculateSignCount());
	}
	
	function showOrHideOpera(index){
		$("#chaozuo_00" + index).css("display") == "none" ? $("#chaozuo_00" + index).show() : $("#chaozuo_00" + index).hide();
	}
	
	getReceiveInfoById = function(page, receiverEmail) {
		var email = $("#email").val();
		var mobile = $("#mobile").val();
		if (receiverEmail == "") {
			return;
		}
		var emailRegExp = new RegExp("^([a-zA-Z0-9]+[_|\_|\.\-]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.\-]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$");
		var mobileRegExp = new RegExp("^[1][0-9]{10}$");

		$("#email_" + page).css("color","");
    	$("#email_" + page).css("border","");
        if (!emailRegExp.test(receiverEmail) && !mobileRegExp.test(receiverEmail)) {
        	$("#email_" + page).css("color","red");
        	$("#email_" + page).css("border","1px solid red");
            return;
        }
	            
		if (email == receiverEmail || mobile == receiverEmail) {
			showAlert("不能添加自己为联系人");
			$("#email_" + page).val("");
			return;
		}
		var postData = "email=" + receiverEmail + "&edit=" + (new Date()).valueOf();
		$.ajax({
			url : "doc!getReceiveInfo.xml",
			type : "POST",
			data : postData,
			dataType : "xml",
			timeout : 300000,
			success : function(data) {
				var result = $(data).find("result >resultMsg").text();
				if (result == null || result == "") {
					//showAlert("系统出错，请稍后重试");
					return;
				}
				var jsonData = JSON.parse(result);
				var errCode = jsonData["errCode"];
				if (errCode > 0) {
					var errShow = new Boolean(jsonData["errShow"]);
					if (errShow == true) {
						$("#email_" + page).val(receiverEmail);
						$("#name_" + page).val("");
						$("#phone_" + page).val("");
						$("#userStatus_" + page).html("未注册签署人");
						/*if(errCode==1007){
							showAlert("此用户不是E签宝注册用户，发起签署后将短信通知此用户注册以完成签署");
						}*/
					} else {
						//showAlert("系统出错，请稍后重试");
					}
					return;
				} else {
					$("#email_" + page).val(receiverEmail);
					if (jsonData["name"] != null) {
						$("#name_" + page).val(jsonData["name"]);
					}
					if (jsonData["accountUUID"] != null) {
						$("#accountUid_" + page).val(jsonData["accountUUID"]);
					}
					if (emailRegExp.test(receiverEmail)) {
						if (jsonData["mobile"] != null) {
							$("#phone_" + page).val(jsonData["mobile"]);
							$("#userStatus_" + page).html("手机+邮箱");
						} else {
							$("#phone_" + page).val("");
							$("#userStatus_" + page).html("邮箱");
						}
					} else {
						if (jsonData["email"] != null) {
							$("#phone_" + page).val(jsonData["email"]);
							$("#userStatus_" + page).html("手机+邮箱");
						} else {
							$("#phone_" + page).val("");
							$("#userStatus_" + page).html("手机");
						}
					}
				}
			}
		});
	}
	
	getImgUrl = function(signdocId, pageCount,show) {
		
		showblockUI();
		var pageNum = pageCount + "-" + (pageCount + 4);
		var postData = "pageNum=" + pageNum + "&signdocId=" + signdocId
				+ "&edit=" + (new Date()).valueOf();
		$.ajax({
			url : "sign!getDocPic.xml",
			type : "POST",
			data : postData,
			dataType : "xml",
			timeout : 300000,
			success : function(data) {
				closeAlert();
				var result = $(data).find("result >resultMsg").text();
				if (result == null || result == "") {
					showAlert("系统出错，请稍后重试");
					return;
				}
				var jsonData = JSON.parse(result);
				var errCode = jsonData["errCode"];
				if (errCode > 0) {
			//		showEventMust("文件解析有误", "deleteDoc(" + signdocId + ")");
//					var errShow = new Boolean(jsonData["errShow"]);
//					if (errShow == true) {
//						showAlert(jsonData["msg"]);
//					} else {
//						showAlert("系统出错，请稍后重试");
//					}
					return;
				} else {
					if(jsonData["pageNum"] == 0){
						showAlert("pdf文件解析异常！");
					}
					$("#before").html("");
					$("#after").html("");
					$(".fileNumber").hide();
					$(".showImage").attr("rel", "");
					var keyArray = jsonData["keys"];
					var key;
					for (var i = 0; i < keyArray.length; i = i + 1) {
						key = keyArray[i];
						if (i == 0 && key["page"] >= 6) {
							$("#before").append("<a onclick='javascript:getImgUrl(" + signdocId + ", " + (key["page"] - 5) + ");'><img src='./images_2.0/left.png' style='padding:70px 0px;cursor:pointer;'></a>");
						} else if (i == (keyArray.length - 1) && key["page"] < 6) {
							$("#before").append("<a onclick='javascript:getImgUrl(" + signdocId + ", " + (key["page"] - 5) + ");'><img src='https://esignweb.oss-cn-hangzhou.aliyuncs.com/imgs/4f7f77b5-105e-4c59-ae4a-d810ac330d1f?Expires=1767576181&OSSAccessKeyId=FBzUaPMorqiiUAfb&Signature=AlAd0I12ROLScYd5hlhHfNrWFOk%3D' style='padding:70px 0px;cursor:pointer;'></a>");
						}
						if (i == 4 && jsonData["pageNum"] > key["page"]) {
							$("#after").append("<a onclick='javascript:getImgUrl(" + signdocId + ", " + (key["page"] + 1) + ");'><img src='./images_2.0/right.png' style='padding:70px 20px;cursor:pointer;'></a>");
						}
						$("#docPage_" + i).show();
						$("#firstImg_" + i).attr("href", key["imageKey"]);
						$("#firstImg_" + i).attr("rel", "group");
						$("#firstImg-a_" + i).attr("src", key["imageKey"]);
						$("#pageNum_" + i).text(key["page"]);
					}
					$("a[rel=group]").fancybox({
						 "type":"image",
						 "showCloseButton":true, 
						 "titlePosition":"inside",
						 "onComplete":function() {lastImage();}
					});
					if (pageCount == 1) {
						$("#pageNum").val(jsonData["pageNum"]);
					}
					if (show == 0) {
						$("#firstImg_0").click()
					} else if (show == 4) {
						$("#firstImg_4").click()
					}
				}
			}
		});
	};
	
	lastImage = function() {
		var ossUrl = $("#fancybox-img").attr("src");
		$("#fancyboxImage").val(ossUrl);
		$("#fancybox-left").attr("pageNum", 0);
		$("#fancybox-right").attr("pageNum", 0);
		$("#fancybox-left-ico").attr("style", "left:20px;");
		$("#fancybox-right-ico").attr("style", "left: auto;right: 20px;");
		$(".showImage").each(function(index){
			if ($(this).attr("href") == ossUrl) {
				var id = $(this).attr("id");
				var length = id.length;
				var num = id.substring(length - 1, length);
				var pageNum = parseInt($("#pageNum_" + num).text());
				if (pageNum != 1 && num == 0) {
					$("#fancybox-left").show();
					$("#fancybox-left").attr("pageNum", pageNum - 5);
				}
				if (pageNum < $("#pageNum").val() && num == 4) {
					$("#fancybox-right").show();
					$("#fancybox-right").attr("pageNum", pageNum + 1);
				}
			}
		});
	}
	
	$("#fancybox-right").click(function(){
		var pageNum = $("#fancybox-right").attr("pageNum");
		if (pageNum > 0) {
			$("#fancybox-right").hide();
			getImgUrl($("#signdocId").val(), pageNum, 0);
		}
	});
	
	$("#fancybox-left").click(function(){
		var pageNum = $("#fancybox-left").attr("pageNum");
		if (pageNum > 0) {
			$("#fancybox-left").hide();
			getImgUrl($("#signdocId").val(), pageNum, 4);
		}
	});
	
	changeSendOrder = function(){
		var orderType = $("#orderType").val();
		if(0==orderType){
			$("#orderTypeImg").attr("src","./images_2.0/gou1.gif");
			$("#orderType").val(1);
			
			$("#signTypeImg").attr("src","./images_2.0/gou2.gif");
			$("#signType").val(0);
		}else{
			$("#orderTypeImg").attr("src","./images_2.0/gou2.gif");
			$("#orderType").val(0);
			
			$("#signTypeImg").attr("src","./images_2.0/gou1.gif");
			$("#signType").val(1);
		}
	}

	
	changeSignType = function(){
		var signType = $("#signType").val();
		if(0==signType){
//			var rows = $(".whiteTopBord").length;
//		    if(rows==1){
//		    	showAlert("收件人数少于两人");
//		    	return;
//		    }
			
			$("#signTypeImg").attr("src","./images_2.0/gou1.gif");
			$("#signType").val(1);
			
			$("#orderTypeImg").attr("src","./images_2.0/gou2.gif");
			$("#orderType").val(0);
		}else{
			$("#signTypeImg").attr("src","./images_2.0/gou2.gif");
			$("#signType").val(0);
			
			$("#orderTypeImg").attr("src","./images_2.0/gou1.gif");
			$("#orderType").val(1);
		}
	}
	
	changeSelfSign = function(){
		var selfSign = $("#selfSign").val();
		if(2!=selfSign){//如果未先行签署
			if(0==selfSign){ //选择先行签署
				$("#billCount_2").html(1);
				$("#selfSignImg").attr("src","./images_2.0/gou1.gif");
				$("#selfSign").val(1);
				//$("#addMe").hide();
				$("#changeSelfSign").hide();
				
			}else{
				$("#billCount_2").html(0);
				//取消选择先行签署
				$("#selfSignImg").attr("src","./images_2.0/gou2.gif");
				$("#selfSign").val(0);
				//$("#addMe").show();
				$("#changeSelfSign").show();
			}
		}else{
			showAlert("您已完成此文件的签署");
		}
		$("#billCount_1").html(calculateSignCount());
	}
	
		function calculateSignCount()
		{
	        //包括自己
			var count = 0;
			$(".select_div").each(function(){
				var text = $(this).text();
	            if (text == "需要签署")
	            {
	                count++;
	            }
			});
	        //自己需要签署
	        if (1 == $("#selfSign").val())
	        {
	            count++;
	       }
	
	        return count;
		}
	saveDoc = function() {
		$("#saveDraft").val(1);
		saveOrNext();
	}
	
	allowDropContact = function(ev) {
	    ev = ev||window.event;
		ev.preventDefault();
		ev.stopPropagation();
	}
	
	dragContact = function(ev,index) {
	 	ev = ev||window.event;
	    ev.stopPropagation();
	    start = ev.clientY;
	    trIndex = index;
	}

	dropContact = function(ev) {
	    ev = ev||window.event;
		ev.preventDefault();
		ev.stopPropagation();
		
		end = ev.clientY;
		
		var moveY = end-start;
		var endIndex = 0;

		var rows = $(".whiteTopBord").length-2;
		
		if(moveY>0&&moveY){
			//移动了几个
			var moverNum = parseInt(moveY/39);
			endIndex = moverNum+trIndex;
			if(endIndex>rows){
				endIndex = rows;
			}
		}
		if(moveY<0&&moveY){
			var moverNum = parseInt(moveY/39);
			endIndex = moverNum+trIndex;
			if(endIndex<1){
				endIndex = 1;
			}
		}
		
		/*var name= $("#name_"+trIndex).val();
		var email = $("#email_"+trIndex).val();
		var phone = $("#phone_"+trIndex).val();
		
		var needVideo = $("#needVideo_"+trIndex).val();
		var needMobile = $("#needMobile_"+trIndex).val();
		var operaType = $("#operaType_"+trIndex).val();
		var moveuserStatus = $("#userStatus_"+trIndex).html();
		
		//先将移动的行移除
		delRow(trIndex);
		var moveTrHtml = $("#trHtmlForAdd").html();
		moveTrHtml = moveTrHtml.replace(/numFlag/g, endIndex);
		moveTrHtml = moveTrHtml.replace("userEmailFlag", email);
		moveTrHtml = moveTrHtml.replace("userNameFlag", name);
		moveTrHtml = moveTrHtml.replace("userPhoneFlag", phone);
		moveTrHtml = moveTrHtml.replace("needVideo_valueFlag", needVideo);
		moveTrHtml = moveTrHtml.replace("userStatusFlag", moveuserStatus);
   		if(needVideo!=1){
   			moveTrHtml = moveTrHtml.replace("video_yes", "video_no");
   		}
   		if(needVideo==2){
   			moveTrHtml = moveTrHtml.replace("doublevideo_no", "doublevideo_yes");
   		}
   		moveTrHtml = moveTrHtml.replace("needMobile_valueFlag", needMobile);
   		if(needMobile!=1){
   			moveTrHtml = moveTrHtml.replace("mobile_yes", "mobile_no");
   		}
   		moveTrHtml = moveTrHtml.replace("operaType_valueFlag", operaType);
		
   		var oldInnerHtml = $("#trTiltleHtmlForAdd").html();
   		
   		var rows = $(".whiteTopBord").length-2;
   		var hasAdd = false;
   		for (var i=1;i<=rows;i++)
      	{
			if(i==endIndex){
				hasAdd = true;
				oldInnerHtml = oldInnerHtml+moveTrHtml;
			}
			var x = i;
			if(hasAdd){
				x = i+1;
			}
			var name= $("#name_"+i).val();
			var email = $("#email_"+i).val();
			var phone = $("#phone_"+i).val();
			
			var needVideo = $("#needVideo_"+i).val();
			var needMobile = $("#needMobile_"+i).val();
			var operaType = $("#operaType_"+i).val();
			var userStatus = $("#userStatus_"+i).html();
			
			var oldTrHtml = $("#trHtmlForAdd").html();
			oldTrHtml = oldTrHtml.replace(/numFlag/g, x);
			
			oldTrHtml = oldTrHtml.replace("userEmailFlag", email);
			oldTrHtml = oldTrHtml.replace("userNameFlag", name);
			oldTrHtml = oldTrHtml.replace("userPhoneFlag", phone);
			oldTrHtml = oldTrHtml.replace("userStatusFlag", userStatus);
			oldTrHtml = oldTrHtml.replace("needVideo_valueFlag", needVideo);
    		if(needVideo!=1){
    			oldTrHtml = oldTrHtml.replace("video_yes", "video_no");
    		}
    		if(needVideo==2){
    			oldTrHtml = oldTrHtml.replace("doublevideo_yes", "doublevideo_no");
    		}
    		
    		
    		oldTrHtml = oldTrHtml.replace("needMobile_valueFlag", needMobile);
    		if(needMobile!=1){
    			oldTrHtml = oldTrHtml.replace("mobile_yes", "mobile_no");
    		}
    		
    		oldTrHtml = oldTrHtml.replace("operaType_valueFlag", operaType);
    		
    		oldInnerHtml = oldInnerHtml+oldTrHtml;
		}
		
   		if(!hasAdd){
   			oldInnerHtml = oldInnerHtml+moveTrHtml;
   		}
		$("#addResseeList").html(oldInnerHtml);
		checkNum();
		for (var i=1;i<=rows+2;i++){
			var operaType = $("#operaType_"+i).val();
			if(operaType!=1){
    			$("#select_div_" + i).html("<img src=\"imgers/fasong1_44.png\" class=\"mr2\"/>接受副本");
    		}
		}
		isAllCheck("video");
		isAllCheck("doubleVideo");
		isAllCheck("mobile");
		
		rows = $(".whiteTopBord").length-2;
		for (var i=1;i<=rows;i++){
			var userStatus = $("#userStatus_"+i).html();
			if("未认证的签署人"==userStatus||"未注册的签署人"==userStatus){
				$("#name_"+i).removeAttr("readonly");
				$("#phone_"+i).removeAttr("readonly");
			}
		}*/
	}
});
